#!/bin/bash

source /home/shell/util.sh

pid=`ps aux|grep geth|grep -v grep|awk '{print $2}'`;

read -p "指定数据目录[/home/eth/node1]：" data_dir
data_dir=${data_dir:-"/home/eth/node1"};

if [ ! -f "/opt/jdk/bin/java" ]; then
    bash /home/shell/java-install.sh
fi
source /etc/profile.d/java.sh

if [ ! -f "/usr/local/go/bin/go" ]; then
    bash /home/shell/golang-install.sh
fi
source /etc/profile.d/go_lang.sh

read -p $'请选择网络类型：\n1. main;\n2. test;\n3. dev;\n[dev default]:' net_type
net_type=${net_type:-"3"};

if [ ! -d "/usr/local/src/go-ethereum/build/bin" ]; then
    cd /usr/local/src
    # git clone https://ghproxy.com/https://github.com/ethereum/go-ethereum.git
    git clone https://gitee.com/mirrors/go-ethereum.git
    cd /usr/local/src/go-ethereum/
    git branch -a
    read -p "选择版本[1.10]：" eth_version
    eth_version=${eth_version:-"1.10"};
    git checkout -b origin/release/${eth_version} remotes/origin/release/${eth_version}
    go env -w GOPROXY=https://goproxy.cn
    go env -w GO111MODULE=auto
    make all
tee /etc/profile.d/eth.sh<<EOF
export PATH="/usr/local/src/go-ethereum/build/bin:\$PATH"
EOF
    source /etc/profile.d/eth.sh

    printInfo "初始化，创建创世块："
    mkdir -p ${data_dir}
    # geth --datadir ${data_dir} account new
    # puppeth
    geth --datadir ${data_dir} init /home/shell/support/ethereum/genesis.json
fi

if [ ! -f "/snap/bin/solc" ]; then
    snap install solc
fi


if [ -n "${pid}" ]; then
    kill -9 $pid
fi
if [ ! -d "/var/log/webapps/ethereum" ]; then
    mkdir -p /var/log/webapps/ethereum
fi

case "${net_type}" in
    1)
        printInfo "使用main网！"
        main_net='--syncmode "fast"'
        break;
    ;;
    2)
        printInfo "使用test网！"
        test_net='--nousb --testnet removedb'
        break;
    ;;
    3)
        printInfo "使用dev网！"
        dev_net='--dev --dev.period 1'
        break;
    ;;
    *)
        printInfo "使用dev网！"
        dev_net='--dev --dev.period 1'
    ;;
esac
# 生成配置文件
geth --identity "zhuchain" --datadir "${data_dir}" --networkid "6554" --rpc --rpccorsdomain "*" --nodiscover --port 30303 --rpcport 8545 --allow-insecure-unlock \
--rpcapi "eth,net,web3,admin,miner,personal" --rpcaddr "0.0.0.0" --rpc.allow-unprotected-txs ${main_net} ${test_net} ${dev_net} dumpconfig > ${data_dir}/geth.toml

# 后台启动
nohup geth --config ${data_dir}/geth.toml >> /var/log/webapps/ethereum/log.log 2>&1 &

firewall-addport 8545
firewall-addport 30303

# 安装web3j
if [ ! -f "/usr/local/web3j-cli/bin/web3j" ]; then
    download /opt/web3j-cli.tar https://ghproxy.com/https://github.com/web3j/web3j-cli/releases/download/v1.4.1/web3j-cli-shadow-1.4.1.tar
    tar -xf /opt/web3j-cli.tar -C /usr/local/
    mv /usr/local/web3j-cli* /usr/local/web3j-cli
    rm -rf /opt/web3j-cli.tar
tee /etc/profile.d/web3j-cli.sh<<EOF
export PATH=\$PATH:/usr/local/web3j-cli/bin
EOF
source /etc/profile.d/web3j-cli.sh
fi

printInfo "=============ethereum=============";
echo "日志：/var/log/webapps/ethereum/log.log"
echo "rpcport:8545"
echo "port:30303"
echo "rpcapi:eth,net,web3,admin,personal"
echo "datadir:${data_dir}"
echo "networkid:20210429"
echo -e "`geth version`"
echo "web3j version:`web3j --version|grep Version|awk '{print $2}'`"
echo "solc version:`solc --version|grep Version|awk '{print $2}'`"
printInfo "=============ethereum=============";